package portal.conf.entity;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.springframework.util.StringUtils;

import portal.common.MyStringUtils;
import portal.conf.QueueConf;

public class QueueEntity extends BaseConf{

	
	/****
	 * Data Map
	 * ****/
	 String QUEUE_NAME    ;// priority
	 int PRIORITY      ;// 43
	 int NICE          ;// 10
	 int QJOB_LIMIT    ;// 10
	 int UJOB_LIMIT    ;// 1
	 int PJOB_LIMIT    ;// 1
	 String RUN_WINDOW;////
	 String r15s;//////// 0.3/1.5
	 String r1m;//////// 0.3/1.5
	 String pg;//////// 4.0/15
	 String it;//////// 10/1
	 String CPULIMIT      ;// 8:0/SunIPC	  8 hours of host model SunIPC
	 long RUNLIMIT;
	 long MEMLIMIT      ;// 5000             jobs bigger than this (5M) will be niced
	 long FILELIMIT     ;// 20000
	 long DATALIMIT     ;// 20000            jobs data segment limit
	 long STACKLIMIT    ;// 2048
	 long CORELIMIT     ;// 20000
	 int PROCLIMIT    ;// 5                job processor limit
	 String USERS         ;// chris alex fred
	 String HOSTS         ;// all
	 String ADMINISTRATORS       ;// chris fred
	 String PRE_EXEC     ;// /usr/local/lsf/misc/testq_pre >> /tmp/pre.out
	 String POST_EXEC    ;// /usr/local/lsf/misc/testq_post |grep -v "Hey"
	 String JOB_STARTER;
	 int REQUEUE_EXIT_VALUES ;// 55 255 78
	 //String bind cpu config "BIND_CPU ;// { BALANCE | PACK }"
	 String BIND_CPU ;// BALANCE
	 String STOP_COND;
	 String DESCRIPTION   ;//  Jobs submitted for this queue are scheduled as urgent\
	 String PREEMPTION;
	 private long RES_REQ_SWAP=0;
	 private long RES_REQ_MEM=0;
	 String RES_REQ="order[ls]";
	 String LIMITED_PROCESS;
	public QueueEntity(){
		
	}
	public QueueEntity(String name){
		this.QUEUE_NAME=name;
	}
	@Override
	public String toString(){
		/*if(StringUtils.isEmpty(QUEUE_NAME))
			return null;
		String value="Begin Queue\n";
		//value+="QUEUE"
		value += QUEUE_NAME ==null?"":("QUEUE_NAME    =    "+QUEUE_NAME+"\n");  ///// priority
		value+=PRIORITY>0?("PRIORITY    =    "+PRIORITY+"\n"):"";     //// 43
		value+=NICE>0?(""+NICE):""         ;//// 10
		value+=QJOB_LIMIT>0?("QJOB_LIMIT    =    "+QJOB_LIMIT+"\n"):""  ;//// 10
		value+=UJOB_LIMIT>0?("UJOB_LIMIT    =    "+UJOB_LIMIT+"\n"):""  ;//// 1
		value+=PJOB_LIMIT>0?("UJOB_LIMIT    =    "+PJOB_LIMIT+"\n"):""  ;//// 1
		//value+=RUN_WINDO
		value+=CPULIMIT ==null?"":("CPULIMIT    =    "+CPULIMIT+"\n");  ;//// 8:0/SunIPC	value+= 8 hours of host model SunIPC
		value+=MEMLIMIT  >0?("MEMLIMIT    =    "+MEMLIMIT+"\n"):"" ;//// 5000           value+= jobs bigger than this (5M) will be niced
		value+=FILELIMIT >0?("FILELIMIT    =    "+FILELIMIT+"\n"):""  ;//// 20000
		value+=DATALIMIT >0?("DATALIMIT    =    "+DATALIMIT+"\n"):""  ;//// 20000          value+= jobs data segment limit
		value+=STACKLIMIT >0?("STACKLIMIT    =    "+STACKLIMIT+"\n"):"" ;//// 2048
		value+=CORELIMIT  >0?("CORELIMIT    =    "+CORELIMIT+"\n"):"" ;//// 20000
		value+=PROCLIMIT  >0?("PROCLIMIT    =    "+PROCLIMIT+"\n"):"" ;//// 5              value+= job processor limit
		value+=USERS   ==null?"":("USERS    =    "+USERS+"\n"); ;//// chris alex fred
		value+=HOSTS   ==null?"":("HOSTS    =    "+HOSTS+"\n"); ;//// all
		value+=ADMINISTRATORS  ==null?"":("ADMINISTRATORS    =    "+ADMINISTRATORS+"\n"); ;//// chris fred
		value+=PRE_EXEC   ==null?"":("PRE_EXEC    =    "+PRE_EXEC+"\n");  ;//// /usr/local/lsf/misc/testq_pre >> /tmp/pre.out
		value+=POST_EXEC  ==null?"":("POST_EXEC    =    "+POST_EXEC+"\n");  ;//// /usr/local/lsf/misc/testq_post |grep -v "Hey"
		value+=REQUEUE_EXIT_VALUES >0?("REQUEUE_EXIT_VALUES    =    "+REQUEUE_EXIT_VALUES+"\n"):"";//// 55 255 78
		//value+=bind cpu config "BIND_CPU ;//// { BALANCE | PACK }"
		value+=BIND_CPU ==null?"":("BIND_CPU    =    "+BIND_CPU+"\n"); ;//// BALANCE
		value+=DESCRIPTION   ==null?"":("DESCRIPTION    =    "+DESCRIPTION+"\n");////  Jobs submitted for this queue are scheduled as urgent\
		value+="End Queue\n";
		
		return value;*/
		return tostring();
	} 
	public String tostring(){
		if(StringUtils.isEmpty(QUEUE_NAME))
			return null;
		String value=QueueConf.getStartTitle()+"\n";
		String [] ele=QueueConf.getElementsP();
		for(String e:ele){
			try {
				Method method= this.getClass().getMethod("get"+MyStringUtils.upperFirstCase(e));
				Object obj=method.invoke(this);
				if(obj==null||obj.toString().length()==0 ||MyStringUtils.isBlank(obj.toString()))
					continue;
				if(obj instanceof Integer ){
					Integer i=(Integer)obj;
					//System.out.println(e+i);
					if(i>0)
						//continue;
						value+= e +"\t\t=\t\t"+i+"\n";
					//continue;
				}else if(obj instanceof Long ){
					Long i=(Long)obj;
					//System.out.println(e+i);
					if(i>0)
						//continue;
						value+= e +"\t\t=\t\t"+i+"\n";
					//continue;
				}else {
					if(!MyStringUtils.isBlank(obj.toString()))
						value+= e +"\t\t=\t\t"+obj.toString()+"\n";
				}
				//if(obj instanceof String)
			} catch (NoSuchMethodException | SecurityException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IllegalAccessException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IllegalArgumentException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (InvocationTargetException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		value+=QueueConf.getEndTitle()+"\n";
		//System.out.println(value);
		return value;
	}
	public String getQUEUE_NAME() {
		return QUEUE_NAME==null?"":QUEUE_NAME;
	}
	public void setQUEUE_NAME(String qUEUE_NAME) {
		QUEUE_NAME = qUEUE_NAME;
	}
	public int getPRIORITY() {
		return PRIORITY;
	}
	public void setPRIORITY(int pRIORITY) {
		PRIORITY = pRIORITY;
	}
	public void setPRIORITY(String pRIORITY) {
		int pInt =0 ;
		try{
			pInt=Integer.parseInt(pRIORITY);
		}catch(NumberFormatException e){
			e.printStackTrace();
		}
		PRIORITY = pInt;
	}
	public int getNICE() {
		return NICE;
	}
	public void setNICE(int nICE) {
		NICE = nICE;
	}
	public void setNICE(String nICE) {
		int pInt =0 ;
		try{
			pInt=Integer.parseInt(nICE);
		}catch(NumberFormatException e){
			e.printStackTrace();
		}
		NICE = pInt;
	}
	public int getQJOB_LIMIT() {
		return QJOB_LIMIT;
	}
	public void setQJOB_LIMIT(int qJOB_LIMIT) {
		QJOB_LIMIT = qJOB_LIMIT;
	}
	public void setQJOB_LIMIT(String qJOB_LIMIT) {
		int pInt =0 ;
		try{
			pInt=Integer.parseInt(qJOB_LIMIT);
		}catch(NumberFormatException e){
			e.printStackTrace();
		}
		QJOB_LIMIT = pInt;
	}
	public int getUJOB_LIMIT() {
		return UJOB_LIMIT;
	}
	public void setUJOB_LIMIT(int uJOB_LIMIT) {
		UJOB_LIMIT = uJOB_LIMIT;
	}
	public void setUJOB_LIMIT(String uJOB_LIMIT) {
		int pInt =0 ;
		try{
			pInt=Integer.parseInt(uJOB_LIMIT);
		}catch(NumberFormatException e){
			e.printStackTrace();
		}
		UJOB_LIMIT = pInt;
	}
	public int getPJOB_LIMIT() {
		return PJOB_LIMIT;
	}
	public void setPJOB_LIMIT(int pJOB_LIMIT) {
		PJOB_LIMIT = pJOB_LIMIT;
	}
	public void setPJOB_LIMIT(String pJOB_LIMIT) {
		int pInt =0 ;
		try{
			pInt=Integer.parseInt(pJOB_LIMIT);
		}catch(NumberFormatException e){
			e.printStackTrace();
		}
		PJOB_LIMIT = pInt;
	}
	public String getCPULIMIT() {
		return CPULIMIT;
	}
	public void setCPULIMIT(String cPULIMIT) {
		CPULIMIT = cPULIMIT;
	}
	public long getMEMLIMIT() {
		return MEMLIMIT;
	}
	public void setMEMLIMIT(long mEMLIMIT) {
		MEMLIMIT = mEMLIMIT;
	}
	public void setMEMLIMIT(String mEMLIMIT) {
		int pInt =0 ;
		try{
			pInt=Integer.parseInt(mEMLIMIT);
		}catch(NumberFormatException e){
			e.printStackTrace();
		}
		MEMLIMIT = pInt;
	}
	public long getFILELIMIT() {
		return FILELIMIT;
	}
	public void setFILELIMIT(long fILELIMIT) {
		FILELIMIT = fILELIMIT;
	}
	public void setFILELIMIT(String fILELIMIT) {
		int pInt =0 ;
		try{
			pInt=Integer.parseInt(fILELIMIT);
		}catch(NumberFormatException e){
			e.printStackTrace();
		}
		FILELIMIT = pInt;
	}
	public long getDATALIMIT() {
		return DATALIMIT;
	}
	public void setDATALIMIT(long dATALIMIT) {
		DATALIMIT = dATALIMIT;
	}
	public void setDATALIMIT(String dATALIMIT) {
		int pInt =0 ;
		try{
			pInt=Integer.parseInt(dATALIMIT);
		}catch(NumberFormatException e){
			e.printStackTrace();
		}
		DATALIMIT = pInt;
	}
	public long getSTACKLIMIT() {
		return STACKLIMIT;
	}
	public void setSTACKLIMIT(long sTACKLIMIT) {
		STACKLIMIT = sTACKLIMIT;
	}
	public void setSTACKLIMIT(String sTACKLIMIT) {
		int pInt =0 ;
		try{
			pInt=Integer.parseInt(sTACKLIMIT);
		}catch(NumberFormatException e){
			e.printStackTrace();
		}
		STACKLIMIT = pInt;
	}
	public long getCORELIMIT() {
		return CORELIMIT;
	}
	public void setCORELIMIT(long cORELIMIT) {
		CORELIMIT = cORELIMIT;
	}
	public void setCORELIMIT(String cORELIMIT) {
		int pInt =0 ;
		try{
			pInt=Integer.parseInt(cORELIMIT);
		}catch(NumberFormatException e){
			e.printStackTrace();
		}
		CORELIMIT = pInt;
	}
	public int getPROCLIMIT() {
		return PROCLIMIT;
	}
	public void setPROCLIMIT(int pROCLIMIT) {
		PROCLIMIT = pROCLIMIT;
	}
	public void setPROCLIMIT(String pROCLIMIT) {
		int pInt =0 ;
		try{
			pInt=Integer.parseInt(pROCLIMIT);
		}catch(NumberFormatException e){
			e.printStackTrace();
		}
		PROCLIMIT = pInt;
	}
	public String getUSERS() {
		return USERS;
	}
	public void setUSERS(String uSERS) {
		USERS = uSERS;
	}
	public String getHOSTS() {
		return HOSTS;
	}
	public void setHOSTS(String hOSTS) {
		HOSTS = hOSTS;
	}
	public String getADMINISTRATORS() {
		return ADMINISTRATORS;
	}
	public void setADMINISTRATORS(String aDMINISTRATORS) {
		ADMINISTRATORS = aDMINISTRATORS;
	}
	public String getPRE_EXEC() {
		return PRE_EXEC;
	}
	public void setPRE_EXEC(String pRE_EXEC) {
		PRE_EXEC = pRE_EXEC;
	}
	public String getPOST_EXEC() {
		return POST_EXEC;
	}
	public void setPOST_EXEC(String pOST_EXEC) {
		POST_EXEC = pOST_EXEC;
	}
	public int getREQUEUE_EXIT_VALUES() {
		return REQUEUE_EXIT_VALUES;
	}
	public void setREQUEUE_EXIT_VALUES(int rEQUEUE_EXIT_VALUES) {
		REQUEUE_EXIT_VALUES = rEQUEUE_EXIT_VALUES;
	}
	public void setREQUEUE_EXIT_VALUES(String rEQUEUE_EXIT_VALUES) {
		int pInt =0 ;
		try{
			pInt=Integer.parseInt(rEQUEUE_EXIT_VALUES);
		}catch(NumberFormatException e){
			e.printStackTrace();
		}
		REQUEUE_EXIT_VALUES = pInt;
	}
	
	public String getBIND_CPU() {
		return BIND_CPU;
	}
	
	public void setBIND_CPU(String bIND_CPU) {
		BIND_CPU = bIND_CPU;
	}
	
	public String getDESCRIPTION() {
		return DESCRIPTION;
	}
	public void setDESCRIPTION(String dESCRIPTION) {
		DESCRIPTION = dESCRIPTION;
	}

	public String getSTOP_COND() {
		return STOP_COND;
	}

	public void setSTOP_COND(String sTOP_COND) {
		STOP_COND = sTOP_COND;
	}
	public String getRUN_WINDOW() {
		return RUN_WINDOW;
	}
	public void setRUN_WINDOW(String rUN_WINDOW) {
		RUN_WINDOW = rUN_WINDOW;
	}
	public String getR15s() {
		return r15s;
	}
	public void setR15s(String r15s) {
		this.r15s = r15s;
	}
	public String getR1m() {
		return r1m;
	}
	public void setR1m(String r1m) {
		this.r1m = r1m;
	}
	public String getPg() {
		return pg;
	}
	public void setPg(String pg) {
		this.pg = pg;
	}
	public String getIt() {
		return it;
	}
	public void setIt(String it) {
		this.it = it;
	}
	public long getRUNLIMIT() {
		return RUNLIMIT;
	}
	public void setRUNLIMIT(long rUNLIMIT) {
		RUNLIMIT = rUNLIMIT;
	}
	public String getPREEMPTION() {
		return PREEMPTION;
	}
	public void setPREEMPTION(String pREEMPTION) {
		PREEMPTION = pREEMPTION;
	}
	public String getJOB_STARTER() {
		return JOB_STARTER;
	}
	public void setJOB_STARTER(String jOB_STARTER) {
		JOB_STARTER = jOB_STARTER;
	}
	
	public long getRES_REQ_SWAP() {
		return RES_REQ_SWAP;
	}
	public long getRES_REQ_MEM() {
		return RES_REQ_MEM;
	}
	public String getRES_REQ() {
		if(RES_REQ_SWAP==0 && RES_REQ_MEM==0)
			return "order[ls]";
		String ret="select[type==any] rusage[";
		if(RES_REQ_SWAP!=0 && RES_REQ_MEM!=0)
			ret+="swp="+RES_REQ_SWAP+":mem="+RES_REQ_MEM+"]";
		if(RES_REQ_SWAP!=0)
			ret+="swp="+RES_REQ_SWAP+"]";
		if(RES_REQ_MEM!=0)
			ret+="mem="+RES_REQ_MEM+"]";
		return ret;
	}
	public void setRES_REQ_SWAP(long rES_REQ_SWAP) {
		RES_REQ_SWAP = rES_REQ_SWAP;
	}
	public void setRES_REQ_MEM(long rES_REQ_MEM) {
		RES_REQ_MEM = rES_REQ_MEM;
	}
	public void setRES_REQ(String rES_REQ) {
		String data =MyStringUtils.noSpace(rES_REQ);
		if(MyStringUtils.isBlank(data)||data.equals("order[ls]"))//默认值
			RES_REQ = "order[ls]";
		else {
			String swap= MyStringUtils.matcher("(swp\\=\\d+)", data);					
			RES_REQ_SWAP= matchLong("(\\d+)",swap);
			String mem = MyStringUtils.matcher("(mem\\=\\d+)", data);
			RES_REQ_MEM = matchLong("(\\d+)",mem);		
		}
	}
	private long matchLong(String reg,String str){
		if(MyStringUtils.isBlank(str))
			return 0L;
		Long l= Long.valueOf(MyStringUtils.matcher(reg, str));
		return l;
	}
	
	public String getLIMITED_PROCESS() {
		return LIMITED_PROCESS;
	}
	public void setLIMITED_PROCESS(String lIMITED_PROCESS) {
		LIMITED_PROCESS = lIMITED_PROCESS;
	}
	@Override
	public String getKey() {
		// TODO Auto-generated method stub
		return getQUEUE_NAME();
	}
	
}
